﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace GoodMan.Infrastructure.Entities.Repository
{
    [Table("T_Integration")]
    public class T_Integration : IEntity<T_Integration>
    {
        [Key]
        [Column(TypeName="varchar")]
        [StringLength(128)]
        public string UserName { get; set; }
        public int Activity { get; set; }
        public int CostValue { get; set; }
        public decimal Balance { get; set; }
        public decimal LockBalance { get; set; }

        public override T_Integration Find(string key)
        {
            return Find(c => c.UserName == key);
        }

        public override T_Integration Find(System.Linq.Expressions.Expression<Func<T_Integration, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Integration.FirstOrDefault(where);
                if (item != null)
                {
                    this.Activity = item.Activity;
                    this.Balance = item.Balance;
                    this.CostValue = item.CostValue;
                    this.UserName = item.UserName;
                    this.LockBalance = item.LockBalance;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_Integration, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.Integration.Any(where);
            }
        }

        public override T_Integration FindLast(System.Linq.Expressions.Expression<Func<T_Integration, bool>> where)
        {
            return Find(where);
        }
    }
}
